from urllib2 import urlopen

import re

from ConnHopper import ConnHopper
from Connection303 import Connection303
from TLD303 import TLDRepo

tr=TLDRepo()

def test1():
	print "Creating Hopper..."
	ch=ConnHopper( 'www.yahoo.com', ListGen1, Valid1 ) #Give it a target link and a method of generating lists
	print "Done.\nRunning..."
	ch.run()#Let it do its work
	print "Done. Reporting..."
	for c in ch.results(): #output results. For a list of 'success' do [ c for c in ch.results() if c.exists() ]
		print c[1],
		print str(c[0])
	return ch
	
def test2():
	print "Creating Hopper..."
	ch=ConnHopper( 'www.yahoo.com', ListGen2, Valid1 )
	print "Done.\nRunning..."
	ch.run()
	print "Done. Reporting..."
	for c in ch.results():
		print c[1],
		print str(c[0])
	return ch

#Returns a list of the target url appended with all TLDs
#from IANA and wikipedia.
def ListGen1( url ):
	outlist = []
	targetconn = Connection303( url, tr )
	for tld in tr.getTLDs():
		outlist.append( Connection303( tr.swapTLD( url, tld ), tr ) )
	return (targetconn, outlist)

#Returns a list of all the links from the target website
def ListGen2( url ):
	def FixUrl(url):
		fixurl = url
		# len("http://") == 7
		if len(url) <= 7:
			fixurl = "http://" + url
		elif url[:7] != "http://":
			fixurl = "http://" + url
		return fixurl
	patLinkFinder = re.compile('href="http://([^"/?]*)"')
	webPage = urlopen(FixUrl(url)).read()
	links = set(re.findall( patLinkFinder, webPage ))
	return (Connection303(url, tr), [ Connection303(l,tr) for l in links ])

#Really ugly validator, placed here just to get the ball rolling
def Valid1( target, hit ):
	aNS=target.getNS()
	bNS=hit.getNS()
	if set(aNS)&set(bNS): #If they share any name hosts, consider them related with 100% confidence
		return 100
	x = set([ tr.NSurl(o[:-1])+'.' for o in aNS ])
	regs = [ re.compile( '.*\.'+ns.replace('.','\.')) for ns in x ] #Create rules based on target NSs. 'ns4.yahoo.com'->*.yahoo.com
	for b in bNS:
		for r in regs:
			if re.match(r,b): #If any of the hit's nameservers match the *.yahoo.com patter of target, 90% sure its related
				return 90
	k = re.compile( '.*'+target.url.split('.')[ tr.fbTLD(target.url) ]+'.*' )
	for b in bNS:
		if re.match(k, b): #finally we will just look for '.*yahoo.*' in the hit's nameservers
			return 50      #Maybe confidence could be higher - it catches stuff like ns8.yahoo.co.jp when given ns.yahoo.com in target
	return 0

